require "src/base/Commander"
require "src/fight/FightConst";
require "src/fight/FightMap";

FightingRole = class("FightingRole", function() 
	return cc.Sprite:create();
end)

function FightRole:ctor(...)
	self.runSpeed = 10;
end

function FightRole:playAttackAction(actionId, opponent)
	
	if self.currActionId == nil then
		self.currActionId = skillId;
		self.currActionFrame = 1;
		self.orginX = self:getPositionX();
		self.orginY = self:getPositionY();
		
		local frameData = self.currActionFrameList[self.currActionFrame];
		self:setPositionX(orginX + frameData.atk.x);
		self:setPositionY(orginY + frameData.atk.y);
		if frameData.ack.rot ~= nil then
			self:setRotation(frameData.ack.rot);
		end
		if self.catchOpo == false and frameData.catchData ~= nil then
			self.catchOpo = self:isCatchOpo(frameData.catchData, opponent);
		end
		if self.catchOpo and frameData.opo ~= nil and frameData.opo.binding == true then
			if frameData.opo.x then
				opponent:setPositionX(orginX+frameData.opo.x);
			end
			if frameData.opo.y then
				opponent:setPositionY(orginY+frameData.opo.y);
			end
			if frameData.opo.actionId ~= nil and frameData.opo.actionFrame then
				opponent:setAction();
			end
			if frameData.opo.rot ~= nil then
				opponent:setRotation(frameData.opo.rot);
			end
			local opoStats = frameData.opo.stats
			if opoStats == FightStats.HITFLY then
				
			elseif opoStats == FightStats.LAYDOWN then
			
			elseif opoStats == FightStats.FALLBACK then
				self.fallBackSpeed = frameData.opo.fallBackSpeed;
			end
			
		end
		
		
		if #self.currActionFrameList == self.currActionFrame then
			--play end
			self.currActionFrameList = nil;
			self.currActionFrame = 0;
			self.currActionId = nil;
		end
	end
end

function FightRole:isCatchOpo(catchData, opo)
	if catchData == nil or opo == nil then
		return false;
	end
	if catchData.maxX >= math.abs(self:getPositionX() - opo:getPositionX()) and 
		catchData.maxY >= math.abs(self:getPositionY() - opo:getPositionY()) then
		return true;
	else 
		return false;
	end
end

function FightingRole:playBaseAction()
	if self.stats == FightStats.STAND then
		self:playFrames(FightStats.STAND);
	elseif self.stats == FightStats.RUN then
		local pX = self:getPositionX();
		if self.walk_dir == 1 then
			pX = pX + self.runSpeed;
		elseif self.walk_dir == -1 then
			px = pX - self.runSpeed;
		end
		self:setPositionX(pX);
	--elseif self.stats == FightStats.ATTACK then
		
	elseif self.stats == FightStats.HURTED then
		
	elseif self.stats == FightStats.JUMPING then
		
	elseif self.stats == FightStats.HITFLY then
		if self.hitFly_speed ~= nil then
			local ox = self:getPositionX();
			local oy = self:getPositionY();
			ox = ox + self.hitFly_speed.x * self.hitFly_dir;
			oy = oy + self.hitFly_speed.y;
			self.hitFly_speed.y = self.hitFly_speed.y - 9.8;
			if self.hitFly_speed.y <= 0 then
				self.hitFly_speed = nil;
				self:playFrames(FightStats.HITFALL);
			end 
		else
			self.stats = FightStats.STAND;
			self:playBaseAction();
		end
	elseif self.stats == FightStats.HITFALL then
		if self.hitFall_speed ~= nil then
			local ox = self:getPositionX();
			local oy = self:getPositionY();
			
		else
			self.stats = FightStats.STAND;
			self:playBaseAction();
		end
	elseif self.stats == FightStats.LAYDOWN then
		local isEnd = self:playFrames(FightStats.LAYDOWN);
		if isEnd then
			self.stats = FightStats.GETUP;
			--self:playBaseAction();
		end
	elseif self.stats == FightStats.FALLBACK then
		local ox = self:getPositionX();
		self:setPositionX(ox - self.fallBackSpeed*self.fallBackDir);
		self.fallBackSpeed = self.fallBackSpeed - 2;
		if self.fallBackSpeed <= 0 then
			self.stats = FightStats.STAND;
			self.fallBackSpeed = 0;
		end
	elseif self.stats == FightStats.GETUP then
		
	--elseif self.stats == FightStats.BECATCH then
	
	else
		self.stats = FightStats.STAND;
		self:playBaseAction();
	end
end